package com.google.android.vending.licensing;

import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteException;
import android.util.Log;
import com.google.android.vending.licensing.util.Base64;
import com.google.android.vending.licensing.util.Base64DecoderException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

public class LicenseChecker
  implements ServiceConnection
{
  private static final boolean DEBUG_LICENSE_ERROR = false;
  private static final String KEY_FACTORY_ALGORITHM = "RSA";
  private static final SecureRandom RANDOM = new SecureRandom();
  private static final String TAG = "LicenseChecker";
  private static final int TIMEOUT_MS = 10000;
  private final Set<LicenseValidator> mChecksInProgress = new HashSet();
  private final Context mContext;
  private Handler mHandler;
  private final String mPackageName;
  private final Queue<LicenseValidator> mPendingChecks = new LinkedList();
  private final Policy mPolicy;
  private PublicKey mPublicKey;
  private ILicensingService mService;
  private final String mVersionCode;

  public LicenseChecker(Context paramContext, Policy paramPolicy, String paramString)
  {
    this.mContext = paramContext;
    this.mPolicy = paramPolicy;
    this.mPublicKey = generatePublicKey(paramString);
    this.mPackageName = this.mContext.getPackageName();
    this.mVersionCode = getVersionCode(paramContext, this.mPackageName);
    HandlerThread localHandlerThread = new HandlerThread("background thread");
    localHandlerThread.start();
    this.mHandler = new Handler(localHandlerThread.getLooper());
  }

  private void cleanupService()
  {
    if (this.mService != null);
    try
    {
      this.mContext.unbindService(this);
      this.mService = null;
      return;
    }
    catch (IllegalArgumentException localIllegalArgumentException)
    {
      while (true)
        Log.e("LicenseChecker", "Unable to unbind from licensing service (already unbound)");
    }
  }

  private void finishCheck(LicenseValidator paramLicenseValidator)
  {
    try
    {
      this.mChecksInProgress.remove(paramLicenseValidator);
      if (this.mChecksInProgress.isEmpty())
        cleanupService();
      return;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  private int generateNonce()
  {
    return RANDOM.nextInt();
  }

  private static PublicKey generatePublicKey(String paramString)
  {
    try
    {
      byte[] arrayOfByte = Base64.decode(paramString);
      PublicKey localPublicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(arrayOfByte));
      return localPublicKey;
    }
    catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
    {
      throw new RuntimeException(localNoSuchAlgorithmException);
    }
    catch (Base64DecoderException localBase64DecoderException)
    {
      Log.e("LicenseChecker", "Could not decode from Base64.");
      throw new IllegalArgumentException(localBase64DecoderException);
    }
    catch (InvalidKeySpecException localInvalidKeySpecException)
    {
      Log.e("LicenseChecker", "Invalid key specification.");
      throw new IllegalArgumentException(localInvalidKeySpecException);
    }
  }

  private static String getVersionCode(Context paramContext, String paramString)
  {
    try
    {
      String str = String.valueOf(paramContext.getPackageManager().getPackageInfo(paramString, 0).versionCode);
      return str;
    }
    catch (PackageManager.NameNotFoundException localNameNotFoundException)
    {
      Log.e("LicenseChecker", "Package not found. could not get version code.");
    }
    return "";
  }

  private void handleServiceConnectionError(LicenseValidator paramLicenseValidator)
  {
    try
    {
      this.mPolicy.processServerResponse(291, null);
      if (this.mPolicy.allowAccess())
        paramLicenseValidator.getCallback().allow(291);
      while (true)
      {
        return;
        paramLicenseValidator.getCallback().dontAllow(291);
      }
    }
    finally
    {
    }
  }

  private void runChecks()
  {
    while (true)
    {
      LicenseValidator localLicenseValidator = (LicenseValidator)this.mPendingChecks.poll();
      if (localLicenseValidator == null)
        break;
      try
      {
        Log.i("LicenseChecker", "Calling checkLicense on service for " + localLicenseValidator.getPackageName());
        this.mService.checkLicense(localLicenseValidator.getNonce(), localLicenseValidator.getPackageName(), new ResultListener(localLicenseValidator));
        this.mChecksInProgress.add(localLicenseValidator);
      }
      catch (RemoteException localRemoteException)
      {
        Log.w("LicenseChecker", "RemoteException in checkLicense call.", localRemoteException);
        handleServiceConnectionError(localLicenseValidator);
      }
    }
  }

  // ERROR //
  public void checkAccess(LicenseCheckerCallback paramLicenseCheckerCallback)
  {
    // Byte code:
    //   0: aload_0
    //   1: monitorenter
    //   2: aload_0
    //   3: getfield 64	com/google/android/vending/licensing/LicenseChecker:mPolicy	Lcom/google/android/vending/licensing/Policy;
    //   6: invokeinterface 228 1 0
    //   11: ifeq +24 -> 35
    //   14: ldc 18
    //   16: ldc_w 296
    //   19: invokestatic 268	android/util/Log:i	(Ljava/lang/String;Ljava/lang/String;)I
    //   22: pop
    //   23: aload_1
    //   24: sipush 256
    //   27: invokeinterface 240 2 0
    //   32: aload_0
    //   33: monitorexit
    //   34: return
    //   35: new 230	com/google/android/vending/licensing/LicenseValidator
    //   38: dup
    //   39: aload_0
    //   40: getfield 64	com/google/android/vending/licensing/LicenseChecker:mPolicy	Lcom/google/android/vending/licensing/Policy;
    //   43: new 298	com/google/android/vending/licensing/NullDeviceLimiter
    //   46: dup
    //   47: invokespecial 299	com/google/android/vending/licensing/NullDeviceLimiter:<init>	()V
    //   50: aload_1
    //   51: aload_0
    //   52: invokespecial 301	com/google/android/vending/licensing/LicenseChecker:generateNonce	()I
    //   55: aload_0
    //   56: getfield 78	com/google/android/vending/licensing/LicenseChecker:mPackageName	Ljava/lang/String;
    //   59: aload_0
    //   60: getfield 84	com/google/android/vending/licensing/LicenseChecker:mVersionCode	Ljava/lang/String;
    //   63: invokespecial 304	com/google/android/vending/licensing/LicenseValidator:<init>	(Lcom/google/android/vending/licensing/Policy;Lcom/google/android/vending/licensing/DeviceLimiter;Lcom/google/android/vending/licensing/LicenseCheckerCallback;ILjava/lang/String;Ljava/lang/String;)V
    //   66: astore_3
    //   67: aload_0
    //   68: getfield 126	com/google/android/vending/licensing/LicenseChecker:mService	Lcom/google/android/vending/licensing/ILicensingService;
    //   71: ifnonnull +103 -> 174
    //   74: ldc 18
    //   76: ldc_w 306
    //   79: invokestatic 268	android/util/Log:i	(Ljava/lang/String;Ljava/lang/String;)I
    //   82: pop
    //   83: aload_0
    //   84: getfield 62	com/google/android/vending/licensing/LicenseChecker:mContext	Landroid/content/Context;
    //   87: new 308	android/content/Intent
    //   90: dup
    //   91: new 211	java/lang/String
    //   94: dup
    //   95: ldc_w 310
    //   98: invokestatic 167	com/google/android/vending/licensing/util/Base64:decode	(Ljava/lang/String;)[B
    //   101: invokespecial 311	java/lang/String:<init>	([B)V
    //   104: invokespecial 312	android/content/Intent:<init>	(Ljava/lang/String;)V
    //   107: aload_0
    //   108: iconst_1
    //   109: invokevirtual 316	android/content/Context:bindService	(Landroid/content/Intent;Landroid/content/ServiceConnection;I)Z
    //   112: ifeq +35 -> 147
    //   115: aload_0
    //   116: getfield 60	com/google/android/vending/licensing/LicenseChecker:mPendingChecks	Ljava/util/Queue;
    //   119: aload_3
    //   120: invokeinterface 319 2 0
    //   125: pop
    //   126: goto -94 -> 32
    //   129: astore 7
    //   131: aload_1
    //   132: bipush 6
    //   134: invokeinterface 322 2 0
    //   139: goto -107 -> 32
    //   142: astore_2
    //   143: aload_0
    //   144: monitorexit
    //   145: aload_2
    //   146: athrow
    //   147: ldc 18
    //   149: ldc_w 324
    //   152: invokestatic 138	android/util/Log:e	(Ljava/lang/String;Ljava/lang/String;)I
    //   155: pop
    //   156: aload_0
    //   157: aload_3
    //   158: invokespecial 111	com/google/android/vending/licensing/LicenseChecker:handleServiceConnectionError	(Lcom/google/android/vending/licensing/LicenseValidator;)V
    //   161: goto -129 -> 32
    //   164: astore 6
    //   166: aload 6
    //   168: invokevirtual 327	com/google/android/vending/licensing/util/Base64DecoderException:printStackTrace	()V
    //   171: goto -139 -> 32
    //   174: aload_0
    //   175: getfield 60	com/google/android/vending/licensing/LicenseChecker:mPendingChecks	Ljava/util/Queue;
    //   178: aload_3
    //   179: invokeinterface 319 2 0
    //   184: pop
    //   185: aload_0
    //   186: invokespecial 329	com/google/android/vending/licensing/LicenseChecker:runChecks	()V
    //   189: goto -157 -> 32
    //
    // Exception table:
    //   from	to	target	type
    //   83	126	129	java/lang/SecurityException
    //   147	161	129	java/lang/SecurityException
    //   2	32	142	finally
    //   35	83	142	finally
    //   83	126	142	finally
    //   131	139	142	finally
    //   147	161	142	finally
    //   166	171	142	finally
    //   174	189	142	finally
    //   83	126	164	com/google/android/vending/licensing/util/Base64DecoderException
    //   147	161	164	com/google/android/vending/licensing/util/Base64DecoderException
  }

  public void onDestroy()
  {
    try
    {
      cleanupService();
      this.mHandler.getLooper().quit();
      return;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  public void onServiceConnected(ComponentName paramComponentName, IBinder paramIBinder)
  {
    try
    {
      this.mService = ILicensingService.Stub.asInterface(paramIBinder);
      runChecks();
      return;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  public void onServiceDisconnected(ComponentName paramComponentName)
  {
    try
    {
      Log.w("LicenseChecker", "Service unexpectedly disconnected.");
      this.mService = null;
      return;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  private class ResultListener extends ILicenseResultListener.Stub
  {
    private static final int ERROR_CONTACTING_SERVER = 257;
    private static final int ERROR_INVALID_PACKAGE_NAME = 258;
    private static final int ERROR_NON_MATCHING_UID = 259;
    private Runnable mOnTimeout;
    private final LicenseValidator mValidator;

    public ResultListener(LicenseValidator arg2)
    {
      Object localObject;
      this.mValidator = localObject;
      this.mOnTimeout = new Runnable()
      {
        public void run()
        {
          Log.i("LicenseChecker", "Check timed out.");
          LicenseChecker.this.handleServiceConnectionError(LicenseChecker.ResultListener.this.mValidator);
          LicenseChecker.this.finishCheck(LicenseChecker.ResultListener.this.mValidator);
        }
      };
      startTimeout();
    }

    private void clearTimeout()
    {
      Log.i("LicenseChecker", "Clearing timeout.");
      LicenseChecker.this.mHandler.removeCallbacks(this.mOnTimeout);
    }

    private void startTimeout()
    {
      Log.i("LicenseChecker", "Start monitoring timeout.");
      LicenseChecker.this.mHandler.postDelayed(this.mOnTimeout, 10000L);
    }

    public void verifyLicense(final int paramInt, final String paramString1, final String paramString2)
    {
      LicenseChecker.this.mHandler.post(new Runnable()
      {
        public void run()
        {
          Log.i("LicenseChecker", "Received response.");
          if (LicenseChecker.this.mChecksInProgress.contains(LicenseChecker.ResultListener.this.mValidator))
          {
            LicenseChecker.ResultListener.this.clearTimeout();
            LicenseChecker.ResultListener.this.mValidator.verify(LicenseChecker.this.mPublicKey, paramInt, paramString1, paramString2);
            LicenseChecker.this.finishCheck(LicenseChecker.ResultListener.this.mValidator);
          }
        }
      });
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.google.android.vending.licensing.LicenseChecker
 * JD-Core Version:    0.6.2
 */